﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Basic4Net.BusinessRules.View;
using Basic4Net.Entities;
using Microsoft.Data.Extensions;

namespace Basic4Net.BusinessRules
{
    public class USWorkList
    {
        private static USWorkList _uSWorkListInstance;
        public static USWorkList USWorkListInstance
        {
            get { return _uSWorkListInstance ?? (_uSWorkListInstance = new USWorkList()); }
        }
         

        public IList<TeachWorkNumList> GetWorkNumInfo(int startAt,int step,string teachCode, string schoolMark,int year,int month,int postId,ref int count)
        {
             using (DBEntities db = new DBEntities())
             {
                 var teachTemp = from t in db.TEACHERINFO
                                 join pr in db.t_TeacherRelation on t.RID equals pr.TeacherID into temp2
                                 from result2 in temp2.DefaultIfEmpty()
                                 join p in db.t_TeacherType on result2.TypeID equals p.RID into temp3
                                 from result3 in temp3.DefaultIfEmpty()
                                 join l in db.t_USWorkList on t.RID equals l.TeacherID into temp
                                 from result in temp.DefaultIfEmpty()  
                                 where (t.SCHOOLMARK == schoolMark && t.ISEFFECT == 1)
                                 && (result2.TypeID.HasValue ? result2.TypeID == postId : true)
                                 && (result.FYear.HasValue ? result.FYear == year : true)
                                 && (result.FMonth.HasValue ? result.FMonth == month : true )
                                 && (string.IsNullOrEmpty(teachCode)?true:(t.TEACHCODE.Contains(teachCode) || t.TEACHNAME.Contains(teachCode)))
                                 orderby t.TEACHCODE descending 
                                 select new TeachWorkNumList
                                            {
                                                TeacherID = t.RID,
                                                TeachName = t.TEACHNAME,
                                                TeachCode = t.TEACHCODE,
                                                PostID = result2.TypeID,
                                                PostName = result3.TypeName,
                                                Sexy = t.SEXY==0?"男":"女",
                                                BirthDay = t.BIRTHDAY,
                                                WorkListID = result.RID == null ? 0 : result.RID,
                                                WorkID = result.WorkID == null ? 0 : result.WorkID,
                                                WorkNum = result.WorkNum == null ? 0 : result.WorkNum,
                                                Year = result.FYear == null ? 0 : result.FYear,
                                                Month = result.FMonth == null ? 0 : result.FMonth
                                            };
                 string s = teachTemp.ToTraceString();
                 count = teachTemp.Count();
                 //IList<TeachWorkNumList> resList = teachTemp.Skip(startAt).Take(step).ToList<TeachWorkNumList>();
                 IList<TeachWorkNumList> resList = teachTemp.ToList<TeachWorkNumList>();
                 return resList;
             }
        }


        public IList<TeachWorkNumList> GetWorkNumHistoryInfo(int startAt, int step, string teachCode, string schoolMark, int year, int month, int postId, ref int count)
        {
            using (DBEntities db = new DBEntities())
            {
                var teachTemp = from t in db.TEACHERINFO
                                join pr in db.t_PostRelation on t.RID equals pr.TeacherID into temp2
                                from result2 in temp2.DefaultIfEmpty()
                                join p in db.t_Post on result2.PostID equals p.RID into temp3
                                from result3 in temp3.DefaultIfEmpty()
                                join l in db.t_USWorkList on t.RID equals l.TeacherID into temp
                                from result in temp.DefaultIfEmpty()
                                where (t.SCHOOLMARK == schoolMark && t.ISEFFECT == 1)
                                && (result2.PostID == postId)
                                && (result.FYear.HasValue?result.FYear == year:false)
                                && (result.FMonth.HasValue?result.FMonth == month:false)
                                && (string.IsNullOrEmpty(teachCode) ? true : (t.TEACHCODE.Contains(teachCode) || t.TEACHNAME.Contains(teachCode)))
                                orderby t.TEACHCODE descending
                                select new TeachWorkNumList
                                {
                                    TeacherID = t.RID,
                                    TeachName = t.TEACHNAME,
                                    TeachCode = t.TEACHCODE,
                                    PostID = result2.PostID,
                                    PostName = result3.PostName,
                                    Sexy = t.SEXY == 0 ? "男" : "女",
                                    BirthDay = t.BIRTHDAY,
                                    WorkListID = result.RID == null ? 0 : result.RID,
                                    WorkID = result.WorkID == null ? 0 : result.WorkID,
                                    WorkNum = result.WorkNum == null ? 0 : result.WorkNum,
                                    Year = result.FYear == null ? 0 : result.FYear,
                                    Month = result.FMonth == null ? 0 : result.FMonth
                                };
                string s = teachTemp.ToTraceString();
                count = teachTemp.Count();
                IList<TeachWorkNumList> resList = teachTemp.Skip(startAt).Take(step).ToList<TeachWorkNumList>();
                return resList;
            }
        }



        /// <summary>
        /// 设置教师课时 若是不存在则插入 存在则修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool SetTeachWorkNum(t_USWorkList entity)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                var b = from l in db.t_USWorkList
                        where l.FYear == entity.FYear && l.FMonth == entity.FMonth && l.TeacherID == entity.TeacherID &&
                        l.WorkID == entity.WorkID && l.SchoolMark == entity.SchoolMark
                        select l;
                if (b.Count() > 0)
                {
                    foreach (var tUsWorkList in b)
                    {
                        tUsWorkList.WorkNum = entity.WorkNum;
                        db.DeleteObject(tUsWorkList);
                        db.AddTot_USWorkList(tUsWorkList);
                    }
                }
                else
                {
                    db.AddTot_USWorkList(entity);
                }
                
                try
                {
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    result = false;
                }
            }
            return result;
        }


        public bool AddTeachWorkNum(t_USWorkList entity)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                var b = from l in db.t_USWorkList
                        where l.FYear == entity.FYear && l.FMonth == entity.FMonth && l.TeacherID == entity.TeacherID &&
                        l.WorkID == entity.WorkID && l.SchoolMark == entity.SchoolMark 
                        select l;
                if (b.Count() > 0)
                    return false;
                db.AddTot_USWorkList(entity);
                try
                {
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    result = false;
                }
            }
            return result;
        }

        public bool UpdateTeachWorkNum(int listId, double workNum)
        {
            bool result = true;
            if (listId <= 0)
                return false;
            using (DBEntities db = new DBEntities())
            {
                var temp = from b in db.t_USWorkList
                           where b.RID == listId
                           select b;

                if (temp.Count() > 0)
                {
                    foreach (var tList in temp)
                    {
                        tList.WorkNum = workNum;
                    }
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception)
                    {
                        result = false;
                    }
                }
            }
            return result;
        }

    }
}
